package com.example.myapplication;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.Toast;

import com.baidu.mapapi.search.core.PoiInfo;
import com.baidu.mapapi.search.poi.OnGetPoiSearchResultListener;
import com.baidu.mapapi.search.poi.PoiDetailResult;
import com.baidu.mapapi.search.poi.PoiDetailSearchResult;
import com.baidu.mapapi.search.poi.PoiIndoorResult;
import com.baidu.mapapi.search.poi.PoiResult;
import com.baidu.mapapi.search.poi.PoiSearch;
import com.baidu.mapapi.map.ArcOptions;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.CircleOptions;
import com.baidu.mapapi.map.InfoWindow;
import com.baidu.mapapi.map.MapPoi;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.map.PolylineOptions;
import com.baidu.mapapi.map.Stroke;
import com.baidu.mapapi.map.TextOptions;
import com.baidu.mapapi.map.UiSettings;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.model.LatLngBounds;
import com.baidu.mapapi.search.busline.BusLineResult;
import com.baidu.mapapi.search.busline.BusLineSearch;
import com.baidu.mapapi.search.busline.BusLineSearchOption;
import com.baidu.mapapi.search.busline.OnGetBusLineSearchResultListener;
import com.baidu.mapapi.search.poi.PoiBoundSearchOption;
import com.baidu.mapapi.search.poi.PoiCitySearchOption;
import com.baidu.mapapi.search.poi.PoiDetailSearchOption;
import com.baidu.mapapi.search.poi.PoiNearbySearchOption;
import com.baidu.mapapi.search.route.BikingRouteResult;
import com.baidu.mapapi.search.route.DrivingRouteResult;
import com.baidu.mapapi.search.route.IndoorRouteResult;
import com.baidu.mapapi.search.route.MassTransitRouteResult;
import com.baidu.mapapi.search.route.OnGetRoutePlanResultListener;
import com.baidu.mapapi.search.route.PlanNode;
import com.baidu.mapapi.search.route.RoutePlanSearch;
import com.baidu.mapapi.search.route.TransitRouteLine;
import com.baidu.mapapi.search.route.TransitRoutePlanOption;
import com.baidu.mapapi.search.route.TransitRouteResult;
import com.baidu.mapapi.search.route.WalkingRouteLine;
import com.baidu.mapapi.search.route.WalkingRoutePlanOption;
import com.baidu.mapapi.search.route.WalkingRouteResult;
import com.baidu.mapapi.search.sug.OnGetSuggestionResultListener;
import com.baidu.mapapi.search.sug.SuggestionResult;
import com.baidu.mapapi.search.sug.SuggestionSearch;
import com.baidu.mapapi.search.sug.SuggestionSearchOption;

import java.util.ArrayList;
import java.util.List;

import overlayutil.BusLineOverlay;
import overlayutil.TransitRouteOverlay;
import overlayutil.WalkingRouteOverlay;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private MapView mMapView = null;
    private BaiduMap mBaiduMap;
    private UiSettings mUiSettings;
    private LatLng mlatLng;
    private AutoCompleteTextView autoctv;
    private SuggestionSearch suggestionSearch;
    private String key;
    private TextWatcher textWatcher;
    private RoutePlanSearch routePlanSearch;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mMapView = (MapView) findViewById(R.id.bmapView);
        mBaiduMap = mMapView.getMap();
        routePlanSearch = RoutePlanSearch.newInstance();
        routePlanSearch.setOnGetRoutePlanResultListener(new OnGetRoutePlanResultListener() {
            @Override
            public void onGetWalkingRouteResult(WalkingRouteResult walkingRouteResult) {
                WalkingRouteLine line = walkingRouteResult.getRouteLines().get(0);
                WalkingRouteOverlay overlay = new WalkingRouteOverlay(mBaiduMap);
                overlay.setData(line);
                overlay.addToMap();
                overlay.zoomToSpan();
            }

            @Override
            public void onGetTransitRouteResult(TransitRouteResult transitRouteResult) {
                TransitRouteLine line = transitRouteResult.getRouteLines().get(0);
                TransitRouteOverlay overlay = new TransitRouteOverlay(mBaiduMap);
                overlay.setData(line);
                overlay.addToMap();
                ;
                overlay.zoomToSpan();

            }

            @Override
            public void onGetMassTransitRouteResult(MassTransitRouteResult massTransitRouteResult) {

            }

            @Override
            public void onGetDrivingRouteResult(DrivingRouteResult drivingRouteResult) {

            }

            @Override
            public void onGetIndoorRouteResult(IndoorRouteResult indoorRouteResult) {

            }

            @Override
            public void onGetBikingRouteResult(BikingRouteResult bikingRouteResult) {

            }
        });

        findViewById(R.id.btn6).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //uid = 14a10620d139f925c8291551
                BusLineSearch busLineSearch = BusLineSearch.newInstance();
                busLineSearch.setOnGetBusLineSearchResultListener(new OnGetBusLineSearchResultListener() {
                    @Override
                    public void onGetBusLineResult(BusLineResult busLineResult) {
                        Log.e(TAG, busLineResult.toString());
                        BusLineOverlay overlay = new BusLineOverlay(mBaiduMap);
                        overlay.setData(busLineResult);
                        overlay.addToMap();
                        overlay.zoomToSpan();
                    }
                });
                busLineSearch.searchBusLine(new BusLineSearchOption().city("广州")
                        .uid("14a10620d139f925c8291551"));

            }
        });


        autoctv = findViewById(R.id.autoctv);
        ArrayList<String> strList = new ArrayList<>();
        strList.add("广东交通职业技术学院");
        strList.add("广东省政府");
        strList.add("广东交通集团");
        strList.add("广东交通厅");
        strList.add("广东省广州市");
        strList.add("广东交通管理");
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                MainActivity.this, android.R.layout.simple_list_item_1, strList);
        autoctv.setAdapter(adapter);

        suggestionSearch = SuggestionSearch.newInstance();
        suggestionSearch.setOnGetSuggestionResultListener(new OnGetSuggestionResultListener() {
            @Override
            public void onGetSuggestionResult(SuggestionResult suggestionResult) {
                List<SuggestionResult.SuggestionInfo> suggestions = suggestionResult.getAllSuggestions();
                ArrayList<String> strList = new ArrayList<>();
                for (int i = 0; i < suggestions.size(); i++) {
                    Log.e(TAG, suggestions.get(i).toString());
                    strList.add(suggestions.get(i).getKey());
                }
                ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                        MainActivity.this, android.R.layout.simple_list_item_1, strList);
                autoctv.setAdapter(adapter);
                autoctv.removeTextChangedListener(textWatcher);
                autoctv.setText(key);
                autoctv.setSelection(key.length());
                autoctv.addTextChangedListener(textWatcher);
            }
        });

        textWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void afterTextChanged(Editable editable) {
                key = editable.toString();
                if (key.length() == 0) {
                    return;
                }
                suggestionSearch.requestSuggestion(new SuggestionSearchOption()
                        .city("广州").keyword(key));
            }
        };
        autoctv.addTextChangedListener(textWatcher);
        findViewById(R.id.btn5).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                suggestionSearch.requestSuggestion(new SuggestionSearchOption()
                        .city("广州").keyword("美食"));
            }
        });

        final PoiSearch poiSearch = PoiSearch.newInstance();
        poiSearch.setOnGetPoiSearchResultListener(new OnGetPoiSearchResultListener() {
            @Override
            public void onGetPoiResult(PoiResult poiResult) {
                List<PoiInfo> poiList = poiResult.getAllPoi();
                for(int i=0; i<poiList.size(); i++){
                    Log.e(TAG, poiList.get(i).toString());
                }

                //创建PoiOverlay对象
                PoiOverlay poiOverlay = new PoiOverlay(mBaiduMap);

                //设置Poi检索数据
                poiOverlay.setData(poiResult);

                //将poiOverlay添加至地图并缩放至合适级别
                poiOverlay.addToMap();
                poiOverlay.zoomToSpan();
            }
            // 废弃
            @Override
            public void onGetPoiDetailResult(PoiDetailResult poiDetailResult) {
            }
            @Override
            public void onGetPoiDetailResult(PoiDetailSearchResult poiDetailSearchResult) {
                Log.e(TAG, poiDetailSearchResult.getPoiDetailInfoList().get(0).toString());
            }
            @Override
            public void onGetPoiIndoorResult(PoiIndoorResult poiIndoorResult) {
            }
        });
        findViewById(R.id.btn1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                poiSearch.searchInCity(new PoiCitySearchOption().city("广州").keyword("84"));
            }
        });
        findViewById(R.id.btn2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                poiSearch.searchNearby(new PoiNearbySearchOption().location(mlatLng)
                        .keyword("美食").radius(1000));
            }
        });
        findViewById(R.id.btn3).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                LatLngBounds latLngBounds = new LatLngBounds.Builder()
                        .include(new LatLng(mlatLng.latitude + 0.05, mlatLng.longitude - 0.05))
                        .include(new LatLng(mlatLng.latitude - 0.05, mlatLng.longitude + 0.05))
                        .build();
                poiSearch.searchInBound(new PoiBoundSearchOption().bound(latLngBounds)
                        .keyword("学校"));
            }
        });
        findViewById(R.id.btn4).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                poiSearch.searchPoiDetail(new PoiDetailSearchOption().poiUids("9fcc825914e5bbe303ec0137"));
            }
        });
        findViewById(R.id.btn7).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                PlanNode start = PlanNode.withLocation(new LatLng(23.188789185719386, 113.3608193901338));
                PlanNode end = PlanNode.withLocation(new LatLng(23.112520094213423, 113.33194785105265));
                routePlanSearch.walkingSearch(new WalkingRoutePlanOption().from(start).to(end));
            }
        });
        findViewById(R.id.btn8).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                PlanNode start = PlanNode.withLocation(new LatLng(23.188789185719386, 113.3608193901338));
                PlanNode end = PlanNode.withLocation(new LatLng(23.112520094213423, 113.33194785105265));
                routePlanSearch.transitSearch(new TransitRoutePlanOption().from(start).to(end).city("广州"));
            }
        });
        mBaiduMap.setOnMapClickListener(new BaiduMap.OnMapClickListener() {
            @Override
            public void onMapClick(LatLng latLng) {
                Log.e(TAG, latLng.toString());
                Toast.makeText(MainActivity.this,
                        "单击 纬度" + latLng.latitude +
                                " 经度 " + latLng.longitude,
                        Toast.LENGTH_SHORT).show();
            }

            @Override
            public boolean onMapPoiClick(MapPoi mapPoi) {
                Toast.makeText(MainActivity.this,
                        "MapPoi单击 " + mapPoi.getName() +
                                " 坐标 " + mapPoi.getPosition().toString(),
                        Toast.LENGTH_SHORT).show();
                return false;
            }
        });

        mBaiduMap.setOnMapDoubleClickListener(new BaiduMap.OnMapDoubleClickListener() {
            @Override
            public void onMapDoubleClick(LatLng latLng) {
                Toast.makeText(MainActivity.this,
                        "单击 纬度" + latLng.latitude +
                                " 经度 " + latLng.longitude,
                        Toast.LENGTH_SHORT).show();
                LatLng p1 = latLng;
                LatLng p2 = new LatLng(p1.latitude - 0.05, p1.longitude);
                LatLng p3 = new LatLng(p1.latitude - 0.05, p1.longitude + 0.05);
                LatLng p4 = new LatLng(p1.latitude, p1.longitude + 0.05);
                List<LatLng> points = new ArrayList<LatLng>();
                points.add(p1);
                points.add(p2);
                points.add(p3);
                points.add(p4);

                //设置折线的属性
                OverlayOptions mOverlayOptions = new PolylineOptions()
                        .width(10)
                        .color(0xAAFF0000)
                        .points(points);
                //在地图上绘制折线
                mBaiduMap.addOverlay(mOverlayOptions);


            }
        });

        mBaiduMap.setOnMapLongClickListener(new BaiduMap.OnMapLongClickListener() {
            @Override
            public void onMapLongClick(LatLng latLng) {
                mlatLng = latLng;
                Toast.makeText(MainActivity.this,
                        "长按 纬度" + latLng.latitude +
                                " 经度 " + latLng.longitude,
                        Toast.LENGTH_SHORT).show();

                drawInfoWindow(latLng);
            }
        });

        mBaiduMap.setOnMarkerClickListener(new BaiduMap.OnMarkerClickListener() {
            @Override
            public boolean onMarkerClick(Marker marker) {
                Toast.makeText(MainActivity.this,
                        marker.getTitle(), Toast.LENGTH_SHORT).show();
                return false;
            }
        });
    }

    private void drawInfoWindow(LatLng latLng) {
        //用来构造InfoWindow的Button
        Button button = new Button(getApplicationContext());
        button.setBackgroundResource(R.mipmap.ic_launcher);
        button.setText("InfoWindow");

//构造InfoWindow
//point 描述的位置点
//-100 InfoWindow相对于point在y轴的偏移量
        InfoWindow mInfoWindow = new InfoWindow(button, latLng, -100);

//使InfoWindow生效
        mBaiduMap.showInfoWindow(mInfoWindow);
    }

    private void drawText(LatLng latLng) {
        //文字覆盖物位置坐标
        LatLng llText = latLng;

//构建TextOptions对象
        OverlayOptions mTextOptions = new TextOptions()
                .text("百度地图SDK") //文字内容
                .bgColor(0xAAFFFF00) //背景色
                .fontSize(24) //字号
                .fontColor(0xFFFF00FF) //文字颜色
                .rotate(-90) //旋转角度
                .position(llText);

//在地图上显示文字覆盖物
        mBaiduMap.addOverlay(mTextOptions);
    }

    private void drawCircle(LatLng latLng) {

        //圆心位置
        LatLng center = latLng;

//构造CircleOptions对象
        CircleOptions mCircleOptions = new CircleOptions().center(center)
                .radius(1400)
                .fillColor(0xAA0000FF) //填充颜色
                .stroke(new Stroke(5, 0xAA00ff00)); //边框宽和边框颜色

//在地图上显示圆
        mBaiduMap.addOverlay(mCircleOptions);
    }

    private void drawArc(LatLng latLng) {
        LatLng p1 = latLng;
        LatLng p2 = new LatLng(p1.latitude - 0.02, p1.longitude + 0.02);
        LatLng p3 = new LatLng(p1.latitude, p1.longitude + 0.04);
        //构造ArcOptions对象
        OverlayOptions mArcOptions = new ArcOptions()
                .color(Color.RED)
                .width(10)
                .points(p1, p2, p3);

        //在地图上显示弧线
        mBaiduMap.addOverlay(mArcOptions);


    }

    private void drawMarker(LatLng latLng) {
        //定义Maker坐标点
        LatLng point = latLng;
        //构建Marker图标
        BitmapDescriptor bitmap = BitmapDescriptorFactory
                .fromResource(R.drawable.marker);
        //构建MarkerOption，用于在地图上添加Marker
        OverlayOptions option = new MarkerOptions()
                .title("故宫博物院")
                .position(point)
                .icon(bitmap);
        //在地图上添加Marker，并显示
        mBaiduMap.addOverlay(option);
    }

    private void drawRect(LatLng latLng) {
        LatLng p1 = latLng;
        LatLng p2 = new LatLng(p1.latitude - 0.05, p1.longitude);
        LatLng p3 = new LatLng(p1.latitude - 0.05, p1.longitude + 0.05);
        LatLng p4 = new LatLng(p1.latitude, p1.longitude + 0.05);
        List<LatLng> points = new ArrayList<LatLng>();
        points.add(p1);
        points.add(p2);
        points.add(p3);
        points.add(p4);
        points.add(p1);

        //设置折线的属性
        OverlayOptions mOverlayOptions = new PolylineOptions()
                .width(10)
                .color(0xAAFF0000)
                .points(points);
        //在地图上绘制折线
        mBaiduMap.addOverlay(mOverlayOptions);
    }

    @Override
    protected void onResume() {
        super.onResume();
        //在activity执行onResume时执行mMapView. onResume ()，实现地图生命周期管理
        mMapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        //在activity执行onPause时执行mMapView. onPause ()，实现地图生命周期管理
        mMapView.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在activity执行onDestroy时执行mMapView.onDestroy()，实现地图生命周期管理
        mMapView.onDestroy();
    }
}
